package com.hiray.demos.ui_architect.litho.list

import android.graphics.Color
import android.graphics.Typeface
import com.facebook.litho.*
import com.facebook.litho.annotations.*
import com.facebook.litho.widget.Text
import com.facebook.yoga.YogaAlign
import com.facebook.yoga.YogaEdge
import com.facebook.yoga.YogaJustify
import com.facebook.yoga.YogaWrap
import com.hiray.demos.R
import com.hiray.demos.ui_architect.litho.custom.LabelView
import com.hiray.demos.ui_architect.litho.image.SingleImageComponent
import com.hiray.demos.ui_architect.litho.image.TrivialImageView

@LayoutSpec
object EleBusinessItemSpec {

    //use StateValue
    @OnCreateInitialState
    fun onInitializeState(c: ComponentContext, state: StateValue<Boolean>, @Prop(optional = true) isOfficialSupport: Boolean) {
        state.set(isOfficialSupport)
    }

    //use State annotation
//    @OnCreateInitialState
//    fun onInitState(c:ComponentContext,@State isChecked:Boolean){
//
//    }

    @OnUpdateState
    fun changeState(state: StateValue<Boolean>, @Param isCheck: Boolean) {
        state.set(isCheck)
    }

    @OnCreateLayout
    fun onCreateLayout(c: ComponentContext, @State state: Boolean, @Prop businessInfo: BusinessInfo): Component {

        return Row.create(c)
                .paddingDip(YogaEdge.ALL, 12f)
                .child(SingleImageComponent.create(c)
                        .image(businessInfo.logo)
                        .resId(0)
                        .autoSize(false)
                        .widthDp(85f)
                        .heightDp(85f)
                        .radius(10f)
                        .flexGrow(0f)
                        .build())
                .child(Column.create(c)
                        .marginDip(YogaEdge.LEFT, 15f)
                        .child(
                                Text.create(c)
                                        .text(businessInfo.businessName)
                                        .textSizeSp(18f)
                                        .textColor(Color.parseColor("#333333"))
                                        .textStyle(Typeface.DEFAULT_BOLD.style)
                                        .build())
                        .child(
                                Row.create(c)
                                        .child(
                                                TrivialImageView.create(c)
                                                        .widthDip(10f)
                                                        .heightDip(10f)
                                                        .imageRes(R.drawable.ic_star_rating)
                                                        .flexGrow(0f)
                                                        .alignSelf(YogaAlign.CENTER)//self star align center
                                                        .build())
                                        .child(Text.create(c)
                                                .text("4.9")
                                                .textSizeSp(12f)
                                                .marginDip(YogaEdge.LEFT, 5f)
                                                .textColor(Color.parseColor("#FE6300"))
                                                .flexGrow(0f)
                                                .alignSelf(YogaAlign.FLEX_END)
                                                .build())
                                        .child(
                                                Text.create(c)
                                                        .text("月售69")
                                                        .marginDip(YogaEdge.LEFT, 10f)
                                                        .textColor(Color.parseColor("#A9A9A9"))
                                                        .flexGrow(0f)
                                                        .textSizeSp(14f)
                                                        .build())
                                        .child(if (businessInfo.isPunctual) Row.create(c)
                                                .child(Row.create(c)
                                                        .child(Text.create(c)
                                                                .text("准时达")
                                                                .textSizeSp(10f)
                                                                .textColor(Color.parseColor("#01A6FF"))
                                                                .paddingDip(YogaEdge.ALL, 2f)
                                                                .build())
                                                        .child(Text.create(c)
                                                                .text("蜂鸟配送")
                                                                .textSizeSp(10f)
                                                                .textColor(Color.WHITE)
                                                                .paddingDip(YogaEdge.ALL, 2f)
                                                                .backgroundColor(Color.parseColor("#01A6FF"))
                                                                .build())
                                                        .border(Border.create(c)
                                                                .widthDip(YogaEdge.ALL, 1f)
                                                                .radiusDip(1f)
                                                                .color(YogaEdge.ALL, Color.parseColor("#01A6FF"))
                                                                .build())
                                                        .build())
                                                .flexGrow(10f)
                                                .justifyContent(YogaJustify.FLEX_END)
                                                .build() else null)
                                        .wrap(YogaWrap.NO_WRAP)
                                        .alignItems(YogaAlign.FLEX_END)
                                        .marginDip(YogaEdge.TOP, 6f)
                                        .build())
                        .child(Text.create(c)
                                .text("起送 20 配送￥4")
                                .textColor(Color.parseColor("#A9A9A9"))
                                .textSizeSp(14f)
                                .marginDip(YogaEdge.TOP, 6f)
                                .build())
                        .child(Row.create(c)//嵌套使用row，否则设置背景后，整行都有效果
                                .child(Text.create(c)
                                        .flexGrow(0f)
                                        .backgroundColor(Color.parseColor("#FEF4EB"))
                                        .text("“" + businessInfo.title + "”")
                                        .textColor(Color.parseColor("#A9A9A9"))
                                        .textSizeSp(14f)
                                        .marginDip(YogaEdge.TOP, 6f)
                                        .build())
                                .build())
                        .child(LabelView.create(c)
                                .labels(businessInfo.promotion)
                                .marginDip(YogaEdge.TOP, 6f)
                                .build())
                        .flexGrow(1f)//填充水平剩余空间
                        .build())
                .build()

    }
}